package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.imp;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.UserDAO;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleUser;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.User;

@Component
public class UserDAOImp implements UserDAO {

	private SessionFactory  sessionFactory;
	  
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
    
	@Autowired(required = true)
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	
	@Override
	public User findByUsername(String username) {
		try {
			Session session = sessionFactory.openSession();
		    Query query = session.createQuery("select u from " + " User u"
		        + " where userName = :usr_name");
		    query.setString("usr_name", username);
		    User ret = (User) query.uniqueResult();
			session.close();
		    return ret;
		}
		catch(HibernateException e){
			
		}
		return null;
	}

	@Override
	public List<Device> getDevices(User userlogged) {
		try {
			Session session = sessionFactory.openSession();
			Transaction tx =  session.beginTransaction();
			session.update(userlogged);
			
			List<Device> ret = null;
			if(userlogged instanceof SimpleUser)
				ret = ((SimpleUser)userlogged).getDevices();
			for (Device device : ret) {
				device.getID();
			}
			tx.commit();
			session.close();
			return ret;
		}
		catch(HibernateException e){
			
		}
		return null;
	}

	@Override
	public List<SimpleUser> listUsers() {
		try {
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			Criteria crit = session.createCriteria(SimpleUser.class);
			crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
			@SuppressWarnings("unchecked")
			List<SimpleUser> ret = crit.list();
			for (SimpleUser user : ret) {
				Hibernate.initialize(user.getDevices());
			}
			tx.commit();
			session.close();
			return ret;
		}
		catch(HibernateException e){
			
		}
		return null;
	}

	@Override
	public void saveOrUpdateUser(SimpleUser user) {
		try {
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(user);
			tx.commit();
			session.close();
		}
		catch(HibernateException e){
			
		}
		
	}

	@Override
	public void deleteUser(SimpleUser user) {
		try {
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.delete(user);
			tx.commit();
			session.close();
		}
		catch(HibernateException e){
			
		}
		
	}

}
